# @Time    : 2022/8/23 9:19
# @Author  : 南黎
# @FileName: 4.键盘监听制作键码查询器.py


#键码查询器，想知道哪个键位的键码就运行程序按下按键。原理是监听按键的按下和弹起并打印键码
from pynput.keyboard import Key, Listener

def on_press(key):
    print('按下的键位是{0}'.format(
        key))

def on_release(key):
    print('弹起的键位是{0}'.format(
        key))
    if key == Key.esc:
        # Stop listener
        print("按esc键退出")
        return False

# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()
